//
//  LEMoreView.m
//  CreditAddressBook
//
//  Created by LE on 16/1/19.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "LEMoreView.h"
#import "LEMoreViewCell.h"
#import "UITableViewCell+Category.h"

@interface LEMoreView ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property(nonatomic, copy) LEMoreBlock block;
@property(nonatomic, weak) UIView *showInView;
@property(nonatomic, weak) UIButton *cover;
@end

@implementation LEMoreView

- (void)setData:(NSArray *)data {
    _data = data;
    
    [self.tableview reloadData];
}

+ (void)showMenuWithData:(NSArray *)data icons:(NSArray *)icons showInView:(UIView *)view didSelectRow:(LEMoreBlock)block {
    LEMoreView *moreView = [LEMoreView shared];
    moreView.alpha = 1;
    moreView.showInView = view;
    moreView.block = [block copy];
    moreView.data = data;
    moreView.icons = icons;
    [moreView prepare];
}

+ (instancetype)shared {
    static LEMoreView *more = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        more = [LEMoreView loadFromNibUsingClassName];
    });
    return more;
}

- (void)prepare {
    CYWeakObject(wself, self);
    static UIButton *cover = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        wself.frame = CGRectMake(wself.showInView.width - wself.width - 8, 64, 0, 0);
    });
    
    cover = [[UIButton alloc] initWithFrame:wself.showInView.frame];
    [cover addTarget:wself action:@selector(coverTouchUp) forControlEvents:UIControlEventTouchUpInside];
    self.cover = cover;
    [wself.showInView addSubview:cover];
    [wself.showInView addSubview:wself];
}

- (void)coverTouchUp {
    if (self.block) {
        self.block(-1);
        self.block = nil;
    }
    CYWeakObject(wself, self);
    [UIView animateWithDuration:.3f animations:^{
        wself.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            [self.cover removeFromSuperview];
        }
    }];
//    self.cover.alpha = 0;
}

- (void)awakeFromNib {
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.backgroundColor = [UIColor clearColor];
    [self.bgImageView setImage:[UIImage resizedImageWithName:@"faduanxin" left:.1 top:.5]];
    self.tableview.bounces = NO;
}

- (void)didSelectRow:(LEMoreBlock)block {
    self.block = [block copy];
}

- (void)setFrame:(CGRect)frame {
    frame.size.width = 150;
    frame.size.height = self.data.count * 40 + 12;
    [super setFrame:frame];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.data[indexPath.row];
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    LEMoreViewCell *cell = [LEMoreViewCell cellWithTableView:tableView];
    cell.title.text = title;
    cell.title.font = [UIFont systemFontOfSize:14];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.icons.count) {
        NSString *icon = nil;
        if (indexPath.row > self.icons.count - 1) {
            icon = self.icons.lastObject;
        } else {
            icon = self.icons[indexPath.row];
        }
        
//        cell.icon.image = [UIImage imageWithImage:[UIImage imageNamed:icon] scaledToSize:CGSizeMake(25, 25)];
        cell.icon.image = [UIImage imageNamed:icon];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.block) {
        self.block(indexPath.row);
        self.block = nil;
    }
    [UIView animateWithDuration:.3f animations:^{
        self.alpha = 0;
        self.cover.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            [self.cover removeFromSuperview];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
@end
